package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.MyToast;
import com.jbm.jbmsupplier.util.Login;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 李征
 * Created by 家帮忙 on 2017/1/12.
 */

public class PhotoLoginPwass extends OvertActivity implements View.OnClickListener {


    @InjectView(R.id.ed_phone_mm)
    EditText edPhoneMm;
    @InjectView(R.id.ed_phone_qrmm)
    EditText edPhoneQrmm;
    public final static int IMPORT_PWA_PHOTO = 0001;
    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    private SharedPreferences mSharedPreferences;
    private DialogLoading mDialog;
    private String Loginname;
    private SharedPreferences.Editor editshar;
    private int loginid;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_import_pwas);
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);

        mSharedPreferences = getSharedPreferences("sinopecall", Activity.MODE_PRIVATE);
        editshar = mSharedPreferences.edit();
        headerTitle.setText("设置密码");
        headerRight.setText("确定");
         loginid = getIntent().getIntExtra("login_id", 0);
    }



    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case IMPORT_PWA_PHOTO: {
                    Login result = (Login) msg.obj;
                    if (result != null) {
                        if (result.login_id > 1) {
                            //验证成功
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            editshar .putString("weinxinlogin","shouji")
                            .putString("phname", Loginname)
                            .putString("phpwd", edPhoneMm.getText().toString())
                            .putBoolean("IsLogin", true).commit();
                           /* .putInt("result.login_id",loginid).commit();*/
                            sendBroadcast(new Intent("isBack").putExtra("login",false).putExtra("relogin","login"));//
                            new RegisterPhotoActivity().finish();
                            finish();
                            MainActivity.goNewHome();

                        } else {
                            //密码设置验证失败
                            if (mDialog != null)
                                mDialog.dismiss();
                            hint(result.errorinfo);
                        }
                    }


                    break;
                }


            }
        }
    };

    @OnClick({R.id.header_right, R.id.header_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_right:
                if (getIntent().getExtras() != null) {
                    if (edPhoneMm.getText().equals("")) {
                        MyToast.show(PhotoLoginPwass.this, "密码不能为空！", Toast.LENGTH_SHORT);
                    } else if (edPhoneMm.getText().toString().length() > 6) {
                        MyToast.show(PhotoLoginPwass.this, "密码长度不能小于六位！", Toast.LENGTH_SHORT);
                    } else if (edPhoneQrmm.getText().toString().equals("")) {
                        MyToast.show(PhotoLoginPwass.this, "二次密码不能为空！", Toast.LENGTH_SHORT);
                    }
                    if (!edPhoneQrmm.getText().toString().equals(edPhoneMm.getText().toString())) {
                        MyToast.show(PhotoLoginPwass.this, "两次密码不一致！", Toast.LENGTH_SHORT);
                    } else {

                        Loginname = getIntent().getStringExtra("ed_phone_sjh");//手机号进行保存
                        NetWork netWork = new NetWork(handler);
                        netWork.getPhotoPwd(loginid, edPhoneMm.getText().toString(), edPhoneQrmm.getText().toString());
                        mDialog = new DialogLoading(this, "验证中...", false);
                        mDialog.show();
                    }}
                break;
            case R.id.header_back:
                finish();
                break;
        }
    }
}
